package com.india.project.push.server;

import com.india.project.push.cachedata.CacheData;
import com.india.project.push.domain.SoHvBarcodeVo;
import com.india.project.push.service.ISoHvBarCodeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class WmsStatisticalServices {
    private static final Logger log = LoggerFactory.getLogger(WmsStatisticalServices.class);
    @Autowired
    private ISoHvBarCodeVo soHvBarcodeService;

    @Scheduled(cron = "0 5,35 * * * ?")
    public void wmsStatistical() throws InterruptedException {
        log.info("+++++++++++++++++++++++++++++++++++++++++ PUNE获取数据定时任务开始 +++++++++++++++++++++++++++++++++++++++++");
        List<SoHvBarcodeVo> soHvBarcodeVos = soHvBarcodeService.searchAll();
        if (soHvBarcodeVos.size() > 0) {
            Iterator<SoHvBarcodeVo> iterator = soHvBarcodeVos.iterator();
            while (iterator.hasNext()) {
                List<SoHvBarcodeVo> it = new ArrayList<>();
                SoHvBarcodeVo soHvBarcodeVo = iterator.next();
                it.add(soHvBarcodeVo);
                CacheData.wms_msg_que.put(it);
            }
            List<String> barcodeList = soHvBarcodeVos.stream().map(SoHvBarcodeVo::getSern).collect(Collectors.toList());
            System.out.println("barcodeList = " + barcodeList);
            soHvBarcodeService.otcWmsPushFlag(barcodeList);
            barcodeList.clear();
        }
        log.info("当前队列长度： " + CacheData.wms_msg_que.size());
        log.info("+++++++++++++++++++++++++++++++++++++++++ PUNE获取数据定时任务结束 +++++++++++++++++++++++++++++++++++++++++");
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    public void otcWmsPush() {
        log.info("================ 开始更新 ================");
        int wmsPush = soHvBarcodeService.otcWmsPush();
        String msg = wmsPush > 0 ? "success" : "error";
        log.info("================ 本次更新数据 " + msg + " 共 " + wmsPush + " 条 ================");
        log.info("================ 更新完成 ================");
    }
}
